﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FTLiteLogic.Interface;
using FullThrustLogic.Core;
using FullThrustLogic.Helper;
using FullThrustLogic.ShipComponent;
using FullThrustLogic.ShipComponent.Weapon;

namespace LogicTest.Controls
{
    public partial class MapControl : UserControl
    {
        private const int MapScale = 20;


        private GameContext _gameContext;
        private Graphics _mapGraphic;
        private Bitmap _map;


        public MapControl(GameContext gameContext)
        {
            InitializeComponent();
            _gameContext = gameContext;


            BuildMapGraphic();

        }

        public void AssignReporting()
        {
            if (_gameContext.Players != null)
            {
                foreach (Player player in _gameContext.Players)
                {
                    foreach (Ship ship in player.Ships)
                    {
                        foreach (IReportableEvent weapon in ship.Weapons)
                        {
                            weapon.EventReporter.OnReportableEvent += new FTLiteLogic.Delegates.ReportableEvent.ReportableEventHandler(EventReporter_OnReportableEvent);
                        }

                        ship.EventReporter.OnReportableEvent += new FTLiteLogic.Delegates.ReportableEvent.ReportableEventHandler(EventReporter_OnReportableEvent);
                    }

                }
            }
        }

        void EventReporter_OnReportableEvent(object sender, FTLiteLogic.Delegates.ReportableEventArgs e)
        {
            txtLog.AppendText(e.EventDescription);
        }

        private void BuildMapGraphic()
        {
            if (_mapGraphic == null)
            {
                if (_gameContext.Map != null)
                {
                    _map = new Bitmap((int) _gameContext.Map.MapWidth*MapScale, (int) _gameContext.Map.MapHeight*MapScale);
                    _map.SetResolution(300, 300);

                    _mapGraphic = Graphics.FromImage(_map);
                }
            }
        }

        public void UpdateMap(Ship selectedShip)
        {
            BuildMapGraphic();

            SolidBrush backGoundColor = new SolidBrush(Color.Black);
            _mapGraphic.FillRectangle(backGoundColor,
                                      0,
                                      0,
                                      (int) _gameContext.Map.MapWidth*MapScale,
                                      (int) _gameContext.Map.MapHeight*MapScale);

            foreach (Player player in _gameContext.Players)
            {
                SolidBrush shipColor;

                SolidBrush shipSelectedColor = new SolidBrush(Color.Yellow);

                int selectGlowSize = 2;

                switch (player.PlayerType)
                {
                    case TypeOfPlayer.Player1:
                        shipColor = new SolidBrush(Color.Blue);
                        break;
                    case TypeOfPlayer.Player2:
                        shipColor = new SolidBrush(Color.Green);
                        
                        break;
                    default:
                        shipColor = new SolidBrush(Color.Red);
                        break;
                }

                foreach (Ship ship in player.Ships)
                {
                    if (ship.Enabled)
                    {

                        ship.ExecuteMovementOrder(false);

                        Point[] shipTriangle = new Point[3];

                        LogicalPoint frontPoint = XYHelper.MovePointByDistanceByDirection(new LogicalPoint((ship.PositionXAfterMoveOrder)*MapScale, (ship.PositionYAfterMoveOrder)*MapScale), ship.Size*MapScale, ship.HeadingAfterMoveOrder);
                        LogicalPoint side1Point = XYHelper.MovePointByDistanceByDirection(new LogicalPoint((ship.PositionXAfterMoveOrder)*MapScale, (ship.PositionYAfterMoveOrder)*MapScale), ship.Size*MapScale, ship.HeadingAfterMoveOrder + 135%360);
                        LogicalPoint side2Point = XYHelper.MovePointByDistanceByDirection(new LogicalPoint((ship.PositionXAfterMoveOrder)*MapScale, (ship.PositionYAfterMoveOrder)*MapScale), ship.Size*MapScale, ship.HeadingAfterMoveOrder - 135%360);

                        shipTriangle[0] = new Point((int) frontPoint.X, (int) frontPoint.Y);
                        shipTriangle[1] = new Point((int) side1Point.X, (int) side1Point.Y);
                        shipTriangle[2] = new Point((int) side2Point.X, (int) side2Point.Y);

                        _mapGraphic.FillPolygon(shipColor, shipTriangle);

                        int scale = MapScale + selectGlowSize;

                        frontPoint = XYHelper.MovePointByDistanceByDirection(new LogicalPoint((ship.PositionX)*MapScale, (ship.PositionY)*MapScale), ship.Size*scale, ship.Heading);
                        side1Point = XYHelper.MovePointByDistanceByDirection(new LogicalPoint((ship.PositionX)*MapScale, (ship.PositionY)*MapScale), ship.Size*scale, ship.Heading + 135%360);
                        side2Point = XYHelper.MovePointByDistanceByDirection(new LogicalPoint((ship.PositionX)*MapScale, (ship.PositionY)*MapScale), ship.Size*scale, ship.Heading - 135%360);

                        if (selectedShip != null && selectedShip == ship)
                        {
                            shipTriangle[0] = new Point((int) frontPoint.X, (int) frontPoint.Y);
                            shipTriangle[1] = new Point((int) side1Point.X, (int) side1Point.Y);
                            shipTriangle[2] = new Point((int) side2Point.X, (int) side2Point.Y);

                            _mapGraphic.FillPolygon(shipSelectedColor, shipTriangle);
                        }

                        frontPoint = XYHelper.MovePointByDistanceByDirection(new LogicalPoint((ship.PositionX)*MapScale, (ship.PositionY)*MapScale), ship.Size*MapScale, ship.Heading);
                        side1Point = XYHelper.MovePointByDistanceByDirection(new LogicalPoint((ship.PositionX)*MapScale, (ship.PositionY)*MapScale), ship.Size*MapScale, ship.Heading + 135%360);
                        side2Point = XYHelper.MovePointByDistanceByDirection(new LogicalPoint((ship.PositionX)*MapScale, (ship.PositionY)*MapScale), ship.Size*MapScale, ship.Heading - 135%360);

                        shipTriangle[0] = new Point((int) frontPoint.X, (int) frontPoint.Y);
                        shipTriangle[1] = new Point((int) side1Point.X, (int) side1Point.Y);
                        shipTriangle[2] = new Point((int) side2Point.X, (int) side2Point.Y);

                        _mapGraphic.FillPolygon(shipColor, shipTriangle);


                        LogicalPoint centerPoint = XYHelper.MovePointByDistanceByDirection(new LogicalPoint((ship.PositionX)*MapScale, (ship.PositionY)*MapScale), ship.Size*MapScale, ship.Heading);
                        LogicalPoint newCenterPoint = XYHelper.MovePointByDistanceByDirection(new LogicalPoint((ship.PositionXAfterMoveOrder)*MapScale, (ship.PositionYAfterMoveOrder)*MapScale), ship.Size*MapScale, ship.HeadingAfterMoveOrder);


                        _mapGraphic.DrawLine(new Pen(shipColor), (int) centerPoint.X, (int) centerPoint.Y,
                                             (int) newCenterPoint.X, (int) newCenterPoint.Y);

                    }
                }

                pbMap.Image = _map;
                pbMap.Height = _map.Height;
                pbMap.Width = _map.Width;
            }
        }

        public void DrawFireArc(Ship ship, LogicalPoint point, iWeapon weapon, double heading)
        {
            UpdateMap(ship);

            Pen pen = new Pen(Color.Green, 1);
            if (weapon.FireArc.Front)
            {
                DrawWeapon(weapon, point, heading, pen, -30, 0, 30);
            }
            
            if (weapon.FireArc.FrontStarboard)
            {
                DrawWeapon(weapon, point, heading, pen, 30, 60, 90);
            }

            if (weapon.FireArc.AftStarboard)
            {
                DrawWeapon(weapon, point, heading, pen, 90, 120, 150);
            }

            if (weapon.FireArc.Aft)
            {
                DrawWeapon(weapon, point, heading, pen, 150, 180, 210);
            }

            if (weapon.FireArc.AftPortside)
            {
                DrawWeapon(weapon, point, heading, pen, 210, 240, 270);
            }

            if (weapon.FireArc.FrontPortside)
            {
                DrawWeapon(weapon, point, heading, pen, 270, 300, 330);
            }

            pbMap.Image = _map;
        }

        private void DrawWeapon(iWeapon weapon, LogicalPoint point, double heading, Pen pen, double leftAngleAddition, double midAngleAddtion, double rightAngleAddition)
        {
            LogicalPoint basePoint = point;
            LogicalPoint leftPoint = XYHelper.MovePointByDistanceByDirection(basePoint, weapon.Range, (heading + leftAngleAddition) % 360);
            LogicalPoint midPoint = XYHelper.MovePointByDistanceByDirection(basePoint, weapon.Range, (heading + midAngleAddtion)%360);
            LogicalPoint rightPoint = XYHelper.MovePointByDistanceByDirection(basePoint, weapon.Range, (heading + rightAngleAddition) % 360);

            _mapGraphic.DrawLine(pen, new Point((int)basePoint.X * MapScale, (int)basePoint.Y * MapScale), new Point((int)leftPoint.X * MapScale, (int)leftPoint.Y * MapScale));
            _mapGraphic.DrawLine(pen, new Point((int)basePoint.X * MapScale, (int)basePoint.Y * MapScale), new Point((int)rightPoint.X * MapScale, (int)rightPoint.Y * MapScale));
            _mapGraphic.DrawLine(pen, new Point((int)leftPoint.X * MapScale, (int)leftPoint.Y * MapScale), new Point((int)midPoint.X * MapScale, (int)midPoint.Y * MapScale));
            _mapGraphic.DrawLine(pen, new Point((int)midPoint.X * MapScale, (int)midPoint.Y * MapScale), new Point((int)rightPoint.X * MapScale, (int)rightPoint.Y * MapScale));
        }
    }
}
